<?php

/**
 * ECSHOP 商品管理程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: goods.php 17217 2011-01-19 06:29:08Z liubo $
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table('comparison_goods'), $db, 'goods_id', 'goods_name');


/*------------------------------------------------------ */
//-- 对比商品列表，
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'goods_list')
{
    //admin_priv('goods_manage');
    $smarty->assign('ur_here',      $_LANG['comparison_goods_list']);
    $smarty->assign('action_link',  array('text' => $_LANG['comparison_goods_add'], 'href' => 'comparison_goods.php?act=goods_add'));
    $smarty->assign('full_page',    1);

    $sql = "select brand_id, brand_name from ".$ecs->table('comparison_brand');
    $brand_list = $db->getAll($sql);
    $smarty->assign('brand',$brand_list);

    $goods_list = get_goodslist();
    foreach($goods_list['goods'] as $key=>$vo){
        $brand_id=$vo['brand_id'];
        $sql = "select brand_name from ".$ecs->table('comparison_brand')." where brand_id=".$brand_id;
        $goods_list['goods'][$key]['brand_id'] = $db->getOne($sql);
    }

    $smarty->assign('goods_list',   $goods_list['goods']);
    $smarty->assign('filter',       $goods_list['filter']);
    $smarty->assign('record_count', $goods_list['record_count']);
    $smarty->assign('page_count',   $goods_list['page_count']);

    assign_query_info();
    $smarty->display('comparison_goods_list.htm');

}
/*------------------------------------------------------ */
//-- 添加对比商品
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'goods_add')
{
    /* 权限判断 */
    //admin_priv('brand_manage');
    $sql = "select brand_id, brand_name from ".$ecs->table('comparison_brand');
    $brand_list = $db->getAll($sql);
    $smarty->assign('brand',$brand_list);

    $smarty->assign('ur_here',     $_LANG['comparison_goods_add']);
    $smarty->assign('action_link', array('text' => $_LANG['comparison_goods_list'], 'href' => 'comparison_goods.php?act=goods_list'));
    $smarty->assign('form_action', 'insert');

    assign_query_info();
    $smarty->display('comparison_goods_add.htm');
}
elseif ($_REQUEST['act'] == 'insert')
{
    /*检查品牌名是否重复*/
    // admin_priv('brand_manage');

    /*处理图片*/
    $img_name = basename($image->upload_image($_FILES['goods_img'],'goodsimg'));

    /*插入数据*/

    $sql = "INSERT INTO ".$ecs->table('comparison_goods')."(goods_name, goods_img, brand_id) ".
        "VALUES ('$_POST[goods_name]', '$img_name', '$_POST[brand_id]')";

    $db->query($sql);

    admin_log($_POST['goods_name'],'add','goods');

    /* 清除缓存 */
    clear_cache_files();

    $link[0]['text'] = '继续添加新商品';
    $link[0]['href'] = 'comparison_goods.php?act=goods_add';

    $link[1]['text'] = '返回对比商品列表';
    $link[1]['href'] = 'comparison_goods.php?act=goods_list';

    sys_msg('新商品添加成功！', 0, $link);
}
/*------------------------------------------------------ */
//-- 编辑商品
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    //admin_priv('brand_manage');
    $sql = "select brand_id, brand_name from ".$ecs->table('comparison_brand');
    $brand_list = $db->getAll($sql);
    $smarty->assign('brand',$brand_list);

    $sql = "SELECT goods_id, goods_name, goods_img, brand_id ".
        "FROM " .$ecs->table('comparison_goods'). " WHERE goods_id='$_REQUEST[id]'";
    $goods = $db->GetRow($sql);

    $smarty->assign('ur_here',     '重新编辑该商品');
    $smarty->assign('action_link', array('text' => $_LANG['comparison_goods_list'], 'href' => 'comparison_goods.php?act=goods_list&' . list_link_postfix()));
    $smarty->assign('goods',       $goods);
    $smarty->assign('form_action', 'updata');

    assign_query_info();
    $smarty->display('comparison_goods_add.htm');
}
elseif ($_REQUEST['act'] == 'updata')
{
    //admin_priv('brand_manage');

    /* 处理图片 */
    $img_name = basename($image->upload_image($_FILES['goods_img'],'goodsimg'));
    $param = "goods_name = '$_POST[goods_name]', brand_id='$_POST[brand_id]' ";
    if (!empty($img_name))
    {
        //有图片上传
        $param .= " ,goods_img = '$img_name' ";
    }

    if ($exc->edit($param,  $_POST['id']))
    {
        /* 清除缓存 */
        clear_cache_files();

        admin_log($_POST['goods_name'], 'edit', 'goods');

        $link[0]['text'] ='返回商品列表';
        $link[0]['href'] = 'comparison_goods.php?act=goods_list&' . list_link_postfix();
        $note = vsprintf('商品修改成功！', $_POST['goods_name']);
        sys_msg($note, 0, $link);
    }
    else
    {
        die($db->error());
    }
}

/*------------------------------------------------------ */
//-- 编辑商品名称
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_goods_name')
{
   // check_authz_json('brand_manage');

    $id     = intval($_POST['id']);
    $name   = json_str_iconv(trim($_POST['val']));

    /* 检查名称是否重复 */

        if ($exc->edit("goods_name = '$name'", $id))
        {
            admin_log($name,'edit','goods');
            make_json_result(stripslashes($name));
        }
        else
        {
            make_json_result(sprintf($_LANG['goodsedit_fail'], $name));
        }

}

/*------------------------------------------------------ */
//-- 删除商品
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    //check_authz_json('brand_manage');

    $id = intval($_GET['id']);

    /* 删除该商品的图标 */
    $sql = "SELECT goods_img FROM " .$ecs->table('comparison_goods'). " WHERE goods_id = '$id'";
    $logo_name = $db->getOne($sql);
    if (!empty($logo_name))
    {
        @unlink(ROOT_PATH . DATA_DIR . '/goodsimg/' .$logo_name);
    }

    $exc->drop($id);

    $url = 'comparison_goods.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 删除商品图片
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'drop_logo')
{
    /* 权限判断 */
   // admin_priv('brand_manage');
    $goods_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

    /* 取得logo名称 */
    $sql = "SELECT goods_img FROM " .$ecs->table('comparison_goods'). " WHERE goods_id = '$goods_id'";
    $logo_name = $db->getOne($sql);

    if (!empty($logo_name))
    {
        @unlink(ROOT_PATH . DATA_DIR . '/goodsimg/' .$logo_name);
        $sql = "UPDATE " .$ecs->table('comparison_goods'). " SET goods_img = '' WHERE goods_id = '$goods_id'";
        $db->query($sql);
    }
    $link= array(array('text' => '商品LOGO删除成功！', 'href' => 'comparison_goods.php?act=edit&id=' . $goods_id), array('text' => '返回商品列表！', 'href' => 'comparison_goods.php?act=goods_list'));
    sys_msg('修改成功！', 0, $link);
}

/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $goods_list = get_goodslist();
    foreach($goods_list['goods'] as $key=>$vo){
        $brand_id=$vo['brand_id'];
        $sql = "select brand_name from ".$ecs->table('comparison_brand')." where brand_id=".$brand_id;
        $goods_list['goods'][$key]['brand_id'] = $db->getOne($sql);
    }
    $smarty->assign('goods_list',   $goods_list['goods']);
    $smarty->assign('filter',       $goods_list['filter']);
    $smarty->assign('record_count', $goods_list['record_count']);
    $smarty->assign('page_count',   $goods_list['page_count']);

    make_json_result($smarty->fetch('comparison_goods_list.htm'), '',
        array('filter' => $goods_list['filter'], 'page_count' => $goods_list['page_count']));
}
/**
 * 获取对比商品列表
 *
 * @access  public
 * @return  array
 */
function get_goodslist()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();
        if(strtoupper(EC_CHARSET) == 'GBK')
        {
            $keyword = iconv("UTF-8", "gb2312", $_POST['goods_name']);
        }
        else
        {
            $keyword = $_POST['goods_name'];
        }

        /* 记录总数以及页数 */
        if (!empty($_POST['goods_name']) && $_POST['brand_id']!='')
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('comparison_goods') ." where goods_name like '%{$keyword}%'"." and brand_id=".$_POST['brand_id'];
        }
        elseif (!empty($_POST['goods_name']))
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('comparison_goods') ." where goods_name like '%{$keyword}%'";
        }
        elseif ($_POST['brand_id']!='')
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('comparison_goods') .' WHERE brand_id='.$_POST['brand_id'];
        }
        else
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('comparison_goods');
        }
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 */
        if (!empty($_POST['goods_name']) && $_POST['brand_id']!='')
        {
            if(strtoupper(EC_CHARSET) == 'GBK')
            {
                $keyword = iconv("UTF-8", "gb2312", $_POST['goods_name']);
            }
            else
            {
                $keyword = $_POST['goods_name'];
            }
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('comparison_goods')." WHERE brand_id=".$_POST['brand_id']." goods_name like '%{$keyword}%'";
        }
        elseif(!empty($_POST['goods_name']))
        {
            if(strtoupper(EC_CHARSET) == 'GBK')
            {
                $keyword = iconv("UTF-8", "gb2312", $_POST['goods_name']);
            }
            else
            {
                $keyword = $_POST['goods_name'];
            }
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('comparison_goods')." WHERE goods_name like '%{$keyword}%'";
        }
        elseif($_POST['brand_id']!='')
        {
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('comparison_goods')."where brand_id=".$_POST['brand_id'];
        }
        else
        {
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('comparison_goods');
        }

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql."  order by goods_id desc", $filter['page_size'], $filter['start']);

    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $goods_img = empty($rows['goods_img']) ? '' :
            '<a href="../' . DATA_DIR . '/goodsimg/'.$rows['goods_img'].'" target="_brank"><img src="images/picflag.gif" width="16" height="16" border="0"/></a>';
        $rows['goods_img'] = $goods_img;

        $arr[] = $rows;
    }

    return array('goods' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
?>